<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 前台首页控制器
 * @author wuliang
 */
class Home extends CI_Controller {
    public function __construct () {
        parent::__construct();
        // 载入模型 model
        $this->load->model('article_model','article');
        $this->load->model('Category_model','cate');
    }

    /**
     * 首页视图
     */
    public function index () {
        $ajax = $this->uri->segment(4);
        // 获得首页推荐文章
        $data = $this->getRecommendArticle($ajax);
        // 读取导航栏目
        $data['category'] = $this->cate->getAll();
        // 读取推荐栏目
        $data['cate'] = $this->getCate();
        // 读取右侧点击排行文章
        $data['click'] = $this->article->getTableArticle('click');
        $data['new'] = $this->article->getTableArticle('new');
        $data['hot'] = $this->article->getTableArticle('hot');
        // 读取右侧图文推荐
        $data['graphic'] = $this->article->getGraphicRecommend();

        $this->load->view('index/home.php',$data);
    }

    /**
     * 读取指定栏目下的推荐文章
     * @param string $ajax
     */
    public function getRecommendArticle ($ajax) {
        $cate = isset($_REQUEST['cid']) ? $_REQUEST['cid'] : '';
        // 加载分页类，对首页推荐文章进行分类
        $perPage = 5;  // 每页显示的条数
        $this->load->helper('page');
        $nowPage = $this->uri->segment(5); // 偏移量,url中代表页数的分段
        $offset = !empty($nowPage) ? ($nowPage-1)*$perPage : $nowPage;
        $this->db->limit($perPage, $offset);
        $data['recommend'] = $this->article->getRecommendArticle($cate);
        $data['link'] = ajaxPageCreate('index/home/getRecommendArticle/ajax',$this->article->countRecommend($cate),$perPage,$nowPage);
        if ($ajax == 'ajax') {
            $this->load->view('index/recommend.php',$data);
        } else {
            return $data;
        }
    }

    /**
     * 获取栏目名称
     * @param int $limit
     */
    public function getCate ($limit = 5) {
        return $this->cate->getName($limit);
    }
}